package com.sundayfactory.workpaychart;

import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends masterActivity implements OnClickListener{
    private final String TAG  = this.getClass().getSimpleName();
    private WheelView WV ;
    private adapter_year adapteryear;
    private DBcontroler dBcontroler;
    private httpAsynk asynk;
    
    private String[] yearList;
    private ListView listView;
    private adapter_paylist adapter_paylist;
    private AdView adView;
    private TextView TV_daydefault;
    private Button   B_link;
    private domain_payinfo info = new domain_payinfo();
    public  callbackInterface callbackInterface = new callbackInterface() {
        
        @Override
        public void onStart(Object o) {
    		
    	
        }
        
        @Override
        public void onComplite(Object o) {
    		paserpaylist list  = (paserpaylist)o;
    		list.startPaser();
    		info = list.info;
    		if( info.getResult() != null && info.getResult().size() > 0  ){
    		   dBcontroler.insert_paydata(info);
    		   UpdateListview(dBcontroler.getYearPayInfo(info.getDetail().getYear()));
    		}else{
   		   Toast.makeText(MainActivity.this, list.Year +" 년도의 데이타가 없습니다.", 1500).show();
   		}
    		
    	
        }
    };
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	
	WV = (WheelView)findViewById(R.id.year);
	WV.setVisibleItems(3);
	dBcontroler = new DBcontroler(this);
	dBcontroler.copyDB(this);
	yearList = dBcontroler.getYearlist();
	
	listView = (ListView)findViewById(R.id.listView1);
	
	
	TV_daydefault = (TextView)findViewById(R.id.textView_day_info);
	TV_daydefault.setText("월기준 작업일");
	B_link = (Button)findViewById(R.id.button1);
	B_link.setText("링크");
	B_link.setOnClickListener(this);
	
	adapteryear = new adapter_year(this, yearList);
	WV.setViewAdapter(adapteryear);
	WV.addScrollingListener(new OnWheelScrollListener() {
	    
	    @Override
	    public void onScrollingStarted(WheelView wheel) {
		
	    }
	    
	    @Override
	    public void onScrollingFinished(WheelView wheel) {
		getpayinfo(WV.getCurrentItem());
	    }
	});
	
	
	// 초기화 
	
	getpayinfo(0);
	
	
	
	// adView 만들기    
	adView = new AdView(this, AdSize.BANNER, "a1517f801079cd3");    
	LinearLayout LL = (LinearLayout)findViewById(R.id.addlayout);
	LL.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	adView.loadAd(new AdRequest());
	
    }
    public void getpayinfo(int year){
	info = dBcontroler.getYearPayInfo(yearList[year]);
	
	if(info.getResult().size() == 0){
	    paserpaylist pl = new paserpaylist(yearList[year]);
		if(asynk != null){
		    asynk.cancel(true);
		}
		asynk = new httpAsynk(callbackInterface);
		asynk.execute(pl);
	}else{
	    UpdateListview(info);
	}
	
	
	
		    
    }
    public void UpdateListview(domain_payinfo items){
	for(domain_Paylist item :items.getResult()){
	    Log.i(TAG, item.getTitle() +"	"+ item.getPay());
	}  
	adapter_paylist = new adapter_paylist(this, R.layout.item_pay, items.getResult());
	listView.setAdapter(adapter_paylist);
	 TV_daydefault.setText(info.getDetail().getYear() + "년의 월평균 근무일수는 "+info.getDetail().getWorkday()+"일로 조사됨.");
		
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }
    @Override
    protected void onDestroy() {
	adView.destroy();    
	super.onDestroy();
    }
    @Override
    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.button1:
	    if(info != null && info.getDetail() != null && info.getDetail().getLink() != null){
		Intent getLink = new Intent(Intent.ACTION_VIEW , Uri.parse(info.getDetail().getLink()));
		
		startActivity(getLink);
	    }
	    
	    
	    break;

	default:
	    break;
	}
    }
}
